cd "C:\Users\Daniele\OneDrive - Università degli Studi di Milano\LAVORI\JEEM Paper\Revision JAERE\Replication files\"

* Figure 1
use "Db contrib pollut sectors - 2000-2015.dta", clear

graph bar (sum) total_Dem (sum) total_Rep (sum) total_Others (sum) total_Ballot if election_year>=2000 & election_year!=2016 , over(election_year, label(angle(ninety))) stack legend(order(1 "Democrats" 2 "Republicans" 3 "Others" 4 "Ballot Committees"))  ytitle(Contributions (Million $))
* Figure 2
use "db for figure 2.dta"
graph bar (sum) total_dem (sum) total_rep (sum) total_others (sum) total_ballot if election_year>=2000 & election_year!=2016 , over(election_year, label(angle(ninety))) stack legend(order(1 "Democrats" 2 "Republicans" 3 "Others" 4 "Ballot Committees"))  ytitle(Contributions (Million $))

* Figure 3
use "Db contrib pollut sectors.dta", clear
graph hbar tot_contr_industry_1000, over( general_industry_2, sort(1) ) title(Contributions from Polluting Sectors) ytitle("Contributions ($ in million)")

* Figure 4
use "db for figure 4.dta" 
bys gov_id : egen mean_ln_envi=mean( ln_total_envi)
twoway scatter mean_ln_envi ln_contr if ln_contr>0, xtitle(Ln(Contributions))  legend(order(1 "Ln(Environmental Expenditures)" 2 "Fitted values")) || lfit mean_ln_envi ln_contr if ln_contr>0 

* Figure 5 - Manipulation Test Cattaneo (2018)
use "DB final estimation.dta", clear

rddensity MV_10, plot graph_options(title( ) xtitle("MV"))

* Figure A.3 - McCrary test
use "DB final estimation.dta", clear

DCdensity MV, breakpoint(0) generate(Xj Yj r0 fhat se_fhat)  

* Figure A.4
use "DB final estimation.dta", clear
global poly_4 "MV MV_2 MV_3 MV_4 dem_MV dem_MV2 dem_MV3 dem_MV4"
global poly_3 "MV MV_2 MV_3 dem_MV dem_MV2 dem_MV3"
global poly_2 "MV MV_2 dem_MV dem_MV2"
global poly_1 "MV dem_MV"

reghdfe ln_envi_exp_real_pc democrat $poly_1 aged kids ln_population ln_income_pc_real  , absorb(id year) vce(cluster id)

collapse (mean) ln_envi_exp_real_pc, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV=democrat*MV_10
eststo: reg ln_envi_exp_real_pc democrat MV_10 democrat_MV 
predict xb
twoway (scatter ln_envi_exp_real_pc MV_10 if MV_10>=-10 & MV_10<=10, xline(0.5) legend(order(1 "Ln(Environmental Expenditures)" 2 "Fitted values")) ) (line xb MV_10 if MV_10<0 & MV_10>=-10 ) (line xb MV_10 if MV_10>0 & MV_10<=10)
drop xb

* II Order

use "DB final estimation.dta", clear
global poly_4 "MV MV_2 MV_3 MV_4 dem_MV dem_MV2 dem_MV3 dem_MV4"
global poly_3 "MV MV_2 MV_3 dem_MV dem_MV2 dem_MV3"
global poly_2 "MV MV_2 dem_MV dem_MV2"
global poly_1 "MV dem_MV"

reghdfe ln_envi_exp_real_pc democrat $poly_1 aged kids ln_population ln_income_pc_real  , absorb(id year) vce(cluster id)

collapse (mean) ln_envi_exp_real_pc, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV=democrat*MV_10
gen democrat_MV2=democrat*MV_10_2
eststo: reg ln_envi_exp_real_pc democrat MV_10 democrat_MV  MV_10_2 democrat_MV2 
predict xb
twoway (scatter ln_envi_exp_real_pc MV_10 if MV_10>=-10 & MV_10<=10, xline(0.5) legend(order(1 "Ln(Environmental Expenditures)" 2 "Fitted values"))) (line xb MV_10 if MV_10<0 & MV_10>=-10 ) (line xb MV_10 if MV_10>0 & MV_10<=10)
drop xb

* III Order
use "DB final estimation.dta", clear
global poly_4 "MV MV_2 MV_3 MV_4 dem_MV dem_MV2 dem_MV3 dem_MV4"
global poly_3 "MV MV_2 MV_3 dem_MV dem_MV2 dem_MV3"
global poly_2 "MV MV_2 dem_MV dem_MV2"
global poly_1 "MV dem_MV"

reghdfe ln_envi_exp_real_pc democrat $poly_1 aged kids ln_population ln_income_pc_real  , absorb(id year) vce(cluster id)

collapse (mean) ln_envi_exp_real_pc, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV=democrat*MV_10
gen democrat_MV2=democrat*MV_10_2
gen democrat_MV3=democrat*MV_10_3

eststo: reg ln_envi_exp_real_pc democrat MV_10 democrat_MV  MV_10_2 democrat_MV2 MV_10_3 democrat_MV3
predict xb
twoway (scatter ln_envi_exp_real_pc MV_10 if MV_10>=-10 & MV_10<=10, xline(0.5) legend(order(1 "Ln(Environmental Expenditures)" 2 "Fitted values"))) (line xb MV_10 if MV_10<0 & MV_10>=-10 ) (line xb MV_10 if MV_10>0 & MV_10<=10) 
drop xb



***********Figure A.5: RDD with Baseline Covariates
***********We aim at showing graphically whether the interaction variables present any discontinuity at the threshold.

****Oil Reserves, I Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) oil_1977_area, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV=democrat*MV_10
gen ln_oil_1977_area=ln(oil_1977_area)
eststo: reg ln_oil_1977_area democrat MV_10 democrat_MV if ln_oil_1977_area>(-5)
predict xb
twoway (scatter ln_oil_1977_area MV_10 if ln_oil_1977_area>(-5) , xtitle(MV) xline(0.5) legend(order(1 "Ln(Oil)" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

****Oil Reserves, III Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) oil_1977_area, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen democrat_MV=democrat*MV_10
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV_2=democrat*MV_10_2
gen democrat_MV_3=democrat*MV_10_3
gen ln_oil_1977_area=ln(oil_1977_area)
reg ln_oil_1977_area democrat MV_10 democrat_MV MV_10_2 democrat_MV_2 MV_10_3 democrat_MV_3 if ln_oil_1977_area>(-5)
predict xb
twoway (scatter ln_oil_1977_area MV_10 if ln_oil_1977_area>(-5) , xtitle(MV) xline(0.5) legend(order(1 "Ln(Oil)" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

****GSP Polluting industries Reserves, I Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) share_GSP_polluting, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV=democrat*MV_10
eststo: reg share_GSP_polluting democrat MV_10 democrat_MV 
predict xb
twoway (scatter share_GSP_polluting MV_10 , xtitle(MV) xline(0.5) legend(order(1 "Share GSP Polluting Industries" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

****GSP Polluting industries Reserves, III Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) share_GSP_polluting, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
replace democrat=0 if MV_10<0
gen democrat_MV=democrat*MV_10
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
gen democrat_MV_2=democrat*MV_10_2
gen democrat_MV_3=democrat*MV_10_3
reg share_GSP_polluting democrat MV_10 democrat_MV MV_10_2 democrat_MV_2 MV_10_3 democrat_MV_3 
predict xb
twoway (scatter share_GSP_polluting MV_10 , xline(0.5) xtitle(MV) legend(order(1 "Share GSP Polluting Industries" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

*****Green Voters, I Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) green_voters, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
replace democrat=0 if MV_10<0
gen democrat_MV=democrat*MV_10
gen democrat_MV_10_2=democrat*MV_10_2
gen democrat_MV_10_3=democrat*MV_10_3
eststo: reg green_voters democrat MV_10 democrat_MV
predict xb
twoway (scatter green_voters MV_10 , xline(0.5) xtitle(MV) legend(order(1 "% Green Voters" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

*****Green Voters, III Order Polynomial
use "DB final estimation.dta", clear
collapse (mean) green_voters, by (MV_10) cw
gen democrat=1 if MV_10>=0 & MV_10!=.
gen MV_10_2=MV_10^2
gen MV_10_3=MV_10^3
replace democrat=0 if MV_10<0
gen democrat_MV=democrat*MV_10
gen democrat_MV_10_2=democrat*MV_10_2
gen democrat_MV_10_3=democrat*MV_10_3
eststo: reg green_voters democrat MV_10 democrat_MV democrat_MV MV_10_2 democrat_MV_10_2 MV_10_3 democrat_MV_10_3 
predict xb
twoway (scatter green_voters MV_10 , xline(0.5) xtitle(MV) legend(order(1 "% Green Voters" 2 "Fitted values"))) (line xb MV_10 if MV_10<0) (line xb MV_10 if MV_10>0)
drop xb

* Figure A.6 Different bandwidths

use "DB final estimation.dta", clear
  clear matrix
  global poly_4 "MV MV_2 MV_3 MV_4 dem_MV dem_MV2 dem_MV3 dem_MV4"

  forvalues MV_10 = 1(1)45 {
       reghdfe ln_envi_exp_real_pc dem_oil democrat  dem_green dem_green_oil dem_term_limit dem_oil_term_limit dem_green_term_limit $poly_4 if MV_10 >= -`MV_10' -1 & MV_10 <= `MV_10' , absorb(id year) vce(cluster id_gov)
	
	*Get number of obs and write to local N
      local N = e(N)
     *Write out the betas as a matrix on a row
      matrix eb = e(b)
      matrix list eb
     *Write out the variances as a row
      matrix se = vecdiag(e(V))
      matrix list eb
     *Write out the variance for just the post variable it is the first as it is the first listed in the reg command	 
      matrix res = (`MV_10',`N',eb[1,1],sqrt(se[1,1]))
      matrix list res
      matrix results_`var' = (nullmat(results_`var') \ res)
   }
   matrix list results_`var' 
   svmat results_`var' 

   gen band = results_1

*Create the confidence intervals

   gen `var'_upper_CI = results_`var'3 + 1.96*results_`var'4
   gen `var'_lower_CI = results_`var'3 - 1.96*results_`var'4


#delimit ;
graph twoway  (line results_3 band, lwidth(heavy) lcolor(black)      ylabel(#3, nogrid))  
			  (line _upper_CI band, lwidth(medium) lcolor(black) clpattern(dot))  
			  (line _lower_CI  band, lwidth(medium) lcolor(black) clpattern(dot))  
			  (line results_3 band, lwidth(heavy) lcolor(blue)   )  
			  (line _upper_CI band, lwidth(medium) lcolor(blue) clpattern(dot))  
			  (line _lower_CI  band, lwidth(medium) lcolor(blue) clpattern(dot))   if band > 1 & band < 45		  
              , title("Env. Expenditures of Democrats in oil rich states", size(4))  
			  subtitle("Robustness to Bandwidth Choice", size(3)) 
			  xtitle("Bandwidth in Margin of Victory") 
			  ytitle("(log) Env. Expenditure") legend(off) 
			  yline(0, lwidth(vthin) lcolor(black))
      ;
#delimit cr
grexportpdf "Appendix A.6"
